import { roleList } from '../db.js'
import pkg from 'mockjs'
const { Random } = pkg
export default [
    {
        url: '/api/role/list',
        method: 'get',
        response: ({ query }) => {
            const { page = 1, size = 10, name, identity, status } = query
            const arr = roleList.filter(item => {
                return (
                    (name ? item.name.indexOf(name) !== -1 : true) &&
                    (identity ? item.identity == identity : true) &&
                    (status ? item.status + '' == status : true)
                )
            })
            // 分页处理
            const pageData = arr.slice((page - 1) * size, page * size)
            return {
                code: 200,
                data: pageData,
                total: arr.length
            }
        },
    },
    {
        url: '/api/role/add',
        method: 'post',
        timeout: 1000,
        response: ({ body }) => {
            const { name, remark, identity } = body
            const newData = {
                id: Random.string('0123456789abcdef', 18),
                name,
                remark,
                identity,
                status: true,
                createtime: Random.datetime()
            }
            roleList.unshift(newData)
            return {
                code: 200,
                data: newData
            }
        },
    },
    {
        url: '/api/role/edit',
        method: 'post',
        timeout: 1000,
        response: ({ body }) => {
            const { id, name, remark, identity } = body
            const index = roleList.findIndex(item => item.id === id)
            if (index !== -1) {
                roleList[index].name = name;
                roleList[index].remark = remark;
                roleList[index].identity = identity;
                return {
                    code: 200,
                    data: roleList[index]
                }
            }
        },
    },
    {
        url: '/api/role/delete',
        method: 'post',
        timeout: 1000,
        response: ({ body }) => {
            const { id } = body
            const index = roleList.findIndex(item => item.id === id)
            if (index !== -1) {
                roleList.splice(index, 1)
                return {
                    code: 200,
                    data: roleList
                }
            }
        },
    }
]